// distributor/pages/applyDistributor/applyDistributor.js
var common = require("../../../common/common.js").common;

var leader = require("../../../common/leader.js").leader;

var WxParse = require("../../../wxParse/wxParse.js");

var {$author: $author} = require("../../../components/base/authorization.js");

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        condition: "",
        name: "",
        phone: "",
        apply_btn: true,
        flag: 1,
        ruleModal: 0,
        pagesLength: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        let that = this;
        let pagesLength = getCurrentPages().length;
        that.setData({
            domain: app.globalData.domain.slice(0, -10),
            pagesLength: pagesLength
        });
        if (options.scene) {
            var scene = decodeURIComponent(options.scene);
            let uid = scene.split(":")[1];
            that.setData({
                uid: uid
            });
        }
        //首次直接进入，拿user_id
                if (app.globalData.user) {
            //如果直接拿到openid
            that.levelShare();
            //海报分享绑定上下级
                } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                that.levelShare();
                //海报分享绑定上下级
                        });
        }
        that.getBase();
        that.getApplyCondition();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息
    getBase: function(openid) {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                var copyText = [];
                if (res.extra.copyright_msg) {
                    var copy = res.extra.copyright_msg;
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                }
                that.setData({
                    color_header: res.extra.color_header,
                    color_text: res.extra.color_text,
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    copyImg: res.extra.copyright_img,
                    copyText: copyText,
                    l_supply: res.extra.l_supply,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    leader_address_on: res.extra.leader_address_on,
                    vip_card_on: res.extra.vip_card_on,
                    new_member_audit: res.extra.new_member_audit,
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2,
                    custom_title: res.extra.level_set.custom_title,
                    level_set: res.extra.level_set,
                    custom_title: res.extra.level_set.custom_title
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
                //首次直接进入，拿user_id
                                if (app.globalData.user) {
                    //如果直接拿到openid
                    if (that.data.uid) {
                        that.levelShare();
                        //海报分享绑定上下级
                                        }
                } else {
                    //回调拿到openid
                    app.userInfoReadyCallback = (res => {
                        if (that.data.uid) {
                            that.levelShare();
                            //海报分享绑定上下级
                                                }
                    });
                }
            }
        });
    },
    //记录搜索名称
    saveWord: function(e) {
        var that = this;
        var key = e.currentTarget.dataset.name;
        var value = e.detail.value;
        that.setData({
            [key]: value
        });
    },
    goBack() {
        wx.navigateBack({});
    },
    levelShare: function() {
        let that = this;
        common.request({
            endpoint: "Level/level/level-share",
            method: "post",
            data: {
                user_id: that.data.uid
            },
            success: res => {
                if (that.data.level_set.success_bind_tip) {
                    wx.showToast({
                        icon: "none",
                        title: that.data.level_set.success_bind_tip
                    });
                } else {
                    wx.showToast({
                        icon: "none",
                        title: "绑定成功"
                    });
                }
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: err.message,
                    showCancel: false
                });
            }
        });
    },
    getApplyCondition: function() {
        let that = this;
        common.request({
            endpoint: "/Level/level/level-apply-condition",
            method: "post",
            data: {},
            success: res => {
                var condition = "";
                var flag = res.extra.flag;
                if (flag != 0) {
                    if (res.extra.con == 3) {
                        //消费次数
                        condition = "已消费" + res.extra.op + "次，距离申请条件还差" + res.extra.detail + "次";
                    }
                    if (res.extra.con == 4) {
                        //金额 
                        condition = "已消费￥" + res.extra.op + "元，距离申请条件还差" + res.extra.detail + "元";
                    }
                    if (res.extra.con == 5) {
                        //金额 
                        condition = "未购买指定商品,请先点击以下任意商品购买！";
                        that.setData({
                            pro: res.extra.pro
                        });
                    }
                }
                that.setData({
                    condition: condition,
                    flag: flag,
                    distraction: WxParse.wxParse("distraction", "html", res.extra.distraction, that, 0),
                    pact: WxParse.wxParse("pact", "html", res.extra.pact, that, 0),
                    info: res.extra
                });
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: err.message,
                    showCancel: false,
                    success(res) {
                        wx.switchTab({
                            url: "/pages/personal/personal"
                        });
                    }
                });
            }
        });
    },
    apply: function() {
        let that = this;
        common.getUserInfoNew({
            success() {
                if (!that.data.name && that.data.info.con != 1) {
                    wx.showModal({
                        title: "提示",
                        content: "请输入申请人姓名!",
                        showCancel: false
                    });
                    return false;
                }
                if (!that.data.phone && that.data.info.con != 1) {
                    wx.showModal({
                        title: "提示",
                        content: "请输入联系电话!",
                        showCancel: false
                    });
                    return false;
                }
                if (!/^[1][0-9]{10}$/.test(that.data.phone) && that.data.info.con != 1) {
                    wx.showModal({
                        title: "提示",
                        content: "请输入正确的手机号码!",
                        showCancel: false
                    });
                    return false;
                }
                that.setData({
                    apply_btn: false
                });
                if (that.data.info.con == 1) {
                    param = {};
                } else {
                    var param = {
                        name: that.data.name,
                        phone: that.data.phone
                    };
                }
                common.request({
                    endpoint: "/Level/level/apply",
                    method: "post",
                    data: param,
                    success: res => {
                        wx.showToast({
                            title: "已提交申请",
                            icon: "none"
                        });
                        setTimeout(() => {
                            wx.switchTab({
                                url: "/pages/personal/personal"
                            });
                        }, 1500);
                    },
                    fail(err) {
                        wx.showModal({
                            title: "提示",
                            content: err.message,
                            showCancel: false
                        });
                        that.setData({
                            apply_btn: true
                        });
                    }
                });
            }
        });
    },
    hideRuleModal: function() {
        let that = this;
        that.setData({
            ruleModal: 0
        });
    },
    showRuleModal: function() {
        let that = this;
        that.setData({
            ruleModal: 1
        });
    },
    goToDetail(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    }
});